<?php

namespace app\api\service;

use app\store\model\AgentArea;
use think\facade\Db;

class AgentService
{
    /**
     * 代理支付订单
     */
    public static function saveTrade($data)
    {
        $money = Db::name('config')->where('key', 'vip_agent_price')->where('store_id', $data['store_id'])->value('value');
        $s_data = [
            'trade_method' => $data['payment_type'],
            'type' => $data['type'],
            'code' => $data['code'],
            'money' => $money,
            'order_no' => $data['order_no'],
            'uid' => $data['uid'],
            'store_id' => $data['store_id'],
            'create_time' => time(),
        ];
        $rst = Db::name('agent_trade_order')->insert($s_data);
        if ($rst) {
            $config = PaymentService::findPaymentConfig($data['store_id'], $data['payment_type']);
            $payment_data = [
                'config' => $config,
                'trade_method' => $data['payment_type'],
                'out_trade_no' => $data['order_no'],
                'real_amount' => $money,
            ];
            if ($data['payment_type'] == 1) {
                $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/agent/wxnotify';
            }
            if ($data['payment_type'] == 2) {
                $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/agent/alinotify';
            }
            $prepay_info = PaymentService::createOrder($payment_data);
            return $prepay_info;
        } else {
            return false;
        }
    }

    /**
     * 代理申请
     */
    public static function applyAgent($data)
    {
        $type = $data['type'];
        $code = $data['code'];
        if ($type == 1) {
            $pid = $code;
            $data['pid'] = $pid;
        }
        if ($type == 2) {
            $cid = $code;
            $pid = Db::name('city')->where('CITY_CODE', $code)->value('PROVINCE_CODE');
            $data['pid'] = $pid;
            $data['cid'] = $cid;
        }
        if ($type == 3) {
            $aid = $code;
            $cid = Db::name('area')->where('AREA_CODE', $code)->value('CITY_CODE');
            $pid = Db::name('city')->where('CITY_CODE', $cid)->value('PROVINCE_CODE');
            $data['pid'] = $pid;
            $data['cid'] = $cid;
            $data['aid'] = $aid;
        }
        // if ($type == 4) {
        //     $pid = $code;
        //     $data['pid'] = $pid;
        // }
        if ($type != 4) {
            $data['create_time'] = time();
            $agentAreaModel = new AgentArea;
            $rst = $agentAreaModel->allowField(['store_id', 'uid', 'type', 'pid', 'cid', 'aid'])->save($data);
            if (!$rst) {
                return false;
            }
            return $agentAreaModel->id;
        } else {
            return true;
        }
    }

    /**
     * 查找代理
     */
    public static function agentInfo($store_id, $uid)
    {
        $agentAreaModel = new AgentArea();
        $rst = $agentAreaModel->where(
            ['uid' => $uid, 'store_id' => $store_id]
        )->find();
        return $rst;
    }

    /**
     * 查找指定类型的代理
     */
    public static function agentInfoByType($store_id, $type, $code)
    {
        $agentAreaModel = new AgentArea();
        $where = ['type' => $type, 'store_id' => $store_id];
        if ($type == 1) {
            $where['pid'] = $code;
        }
        if ($type == 2) {
            $where['cid'] = $code;
        }
        if ($type == 3) {
            $where['aid'] = $code;
        }
        if ($type == 4) {
            $where['pid'] = $code;
        }
        $rst = $agentAreaModel->where($where)->where('status', '<>', 3)->find();
        return $rst;
    }

    /**
     * 代理用户列表
     */
    public static function userList($store_id, $code, $type)
    {
        $where = ['a.store_id' => $store_id, 'a.type' => $type, 'a.delete_time' => 0, 'a.status' => 2];
        if ($type == 1) {
            $where['a.pid'] = $code;
            $where['a.type'] = 1;
        }
        if ($type == 2) {
            $where['a.cid'] = $code;
            $where['a.type'] = 2;
        }
        if ($type == 3) {
            $where['a.aid'] = $code;
            $where['a.type'] = 3;
        }
        if ($type == 4) {
            $where['a.pid'] = $code;
            $where['a.type'] = 4;
        }
        $list = Db::name('agent_area')->alias('a')
            ->join('user u', 'a.uid = u.user_id', 'left')
            ->join('user_auth ua', 'ua.id = u.user_id', 'left')
            ->where($where)
            ->field('u.user_id,u.nickname,u.avatar,u.is_agent,ua.phone,ua.register_time')
            ->paginate(20)->each(function ($item) {
            $item['phone'] = substr_replace($item['phone'], '****', 3, 4);
            $item['register_num'] = format_date($item['register_time']);
            $item['register_time'] = date('Y-m-d H:i:s', $item['register_time']);
            return $item;
        });
        return $list;
    }

    /**
     * 根据代理类型查 名称
     */
    public static function nameList($store_id, $PROVINCE_CODE, $type)
    {
        $nameArr = [];
        if ($type == 2) { //市代
            $nameArr = Db::name('city')->where('PROVINCE_CODE', $PROVINCE_CODE)->field('CITY_CODE as CODE,CITY_NAME as NAME')
                ->paginate(20)->each(function ($item) use ($store_id, $type) {
                $agentAreaModel = new AgentArea();
                $c_count = $c_count = $agentAreaModel->where(['type' => 2, 'status' => 2, 'cid' => $item['CODE'], 'store_id' => $store_id])->count(); //市代数量
                $item['count'] = $c_count;
                $item['type'] = $type;
                return $item;
            });
        }

        if ($type == 3) { //区/县代
            $cityCode = Db::name('city')->where('PROVINCE_CODE', $PROVINCE_CODE)->field('CITY_CODE')->select()->toArray();
            $city_code = array_column($cityCode, 'CITY_CODE');
            $nameArr = Db::name('area')->where('CITY_CODE', 'in', $city_code)->field('AREA_CODE as CODE,AREA_NAME as NAME')->paginate(20)
                ->each(function ($item) use ($store_id, $type) {
                    $agentAreaModel = new AgentArea();
                    $c_count = $c_count = $agentAreaModel->where(['type' => 3, 'status' => 2, 'aid' => $item['CODE'], 'store_id' => $store_id])->count(); //市代数量
                    $item['count'] = $c_count;
                    $item['type'] = $type;
                    return $item;
                });
        }

        // if ($type == 4) {
        //     $cityCode = Db::name('city')->where('PROVINCE_CODE', $PROVINCE_CODE)->field('CITY_CODE')->select()->toArray();
        //     $city_code = array_column($cityCode, 'CITY_CODE');
        //     $areaCode = Db::name('area')->where('CITY_CODE', 'in', $city_code)->field('AREA_CODE')->select()->toArray();
        //     $area_code = array_column($areaCode, 'AREA_CODE');
        //     $nameArr =  Db::name('street')->where('AREA_CODE', 'in', $area_code)->field('STREET_CODE,STREET_NAME')->select()->toArray();
        // }
        return $nameArr;
    }

    /**
     * 省下单独的代理数量
     */
    public static function provinceAgent($store_id, $PROVINCE_CODE)
    {
        $agentAreaModel = new AgentArea();
        $p_count = $agentAreaModel->where(['type' => 1, 'status' => 2, 'pid' => $PROVINCE_CODE, 'store_id' => $store_id])->count(); //省代数量
        $c_count = $agentAreaModel->where(['type' => 2, 'status' => 2, 'pid' => $PROVINCE_CODE, 'store_id' => $store_id])->count(); //市代数量
        $a_count = $agentAreaModel->where(['type' => 3, 'status' => 2, 'pid' => $PROVINCE_CODE, 'store_id' => $store_id])->count(); //区/县代数量
        $v_count = $agentAreaModel->where(['type' => 4, 'status' => 2, 'pid' => $PROVINCE_CODE, 'store_id' => $store_id])->count(); //vip代理数量
        return [$p_count, $c_count, $a_count, $v_count];
    }

    /**
     * 全国代理列表
     */
    public static function getAllList($store_id)
    {
        $agentAreaModel = new AgentArea();
        $p_count = $agentAreaModel->where(['type' => 1, 'status' => 2, 'store_id' => $store_id])->count(); //省代数量
        $c_count = $agentAreaModel->where(['type' => 2, 'status' => 2, 'store_id' => $store_id])->count(); //市代数量
        $a_count = $agentAreaModel->where(['type' => 3, 'status' => 2, 'store_id' => $store_id])->count(); //区/县代数量
        $v_count = $agentAreaModel->where(['type' => 4, 'status' => 2, 'store_id' => $store_id])->count(); //vip代理数量

        $provinceArr = Db::name('province')->field('PROVINCE_CODE,PROVINCE_NAME')->select()->toArray();
        foreach ($provinceArr as $k => $v) {
            $temp = [];
            $temp['p_count'] = $agentAreaModel->where(['type' => 1, 'status' => 2, 'pid' => $v['PROVINCE_CODE'], 'store_id' => $store_id])->count(); //省代数量
            $temp['c_count'] = $agentAreaModel->where(['type' => 2, 'status' => 2, 'pid' => $v['PROVINCE_CODE'], 'store_id' => $store_id])->count(); //市代数量
            $temp['a_count'] = $agentAreaModel->where(['type' => 3, 'status' => 2, 'pid' => $v['PROVINCE_CODE'], 'store_id' => $store_id])->count(); //区/县代数量
            $temp['v_count'] = $agentAreaModel->where(['type' => 4, 'status' => 2, 'pid' => $v['PROVINCE_CODE'], 'store_id' => $store_id])->count(); //vip代理数量
            $provinceArr[$k]['count'] = $temp;
        }
        return [$p_count, $c_count, $a_count, $v_count, $provinceArr];
    }
}
